<?php
class AdminModule extends BaseModel{
    public $table = 'admin';
    public $_table = 'admin';
    public $_table_menu = 'admin_menu';
    public $_table_admin_role = 'admin_role';

	public function __construct($db_choose = 'default'){
		parent::__construct($db_choose);
	}

    public function getUserInfoByUserIDS($userid, $fileds = ''){
        if(empty($userid)){
            return false;
        }

        if(empty($fileds)){
            $fileds = 'adminid,name,account';
        }

        $sql = "select {$fileds} from {$this->_table} where adminid in (".$userid.")";
        $this->_db->prepare($sql);
        if($this->_db->execute()){
            return $this->_db->fetchAll();
        }
        return null;
    }

    public function getUserInfoByPhone($phone, $fileds = '*'){
        if(empty($phone)){
            return false;
        }

        $sql = "select {$fileds} from {$this->_table} where phone='{$phone}'";
        $this->_db->prepare($sql);
        if($this->_db->execute()){
            return $this->_db->fetch();
        }
        return null;
    }

    public function getAllMenu($fileds, $pid, $type = null, $isshow = 'Y'){
        if(empty($fileds)){
            $fileds = '*';
        }

        $where = '';
        if($type){
            $where = ' and type=:type ';
        }
        if($isshow){
            $where .= ' and isshow=:isshow ';
        }

        $flag = false;
        if(! AppCommon::intParse($pid, $parentid) || $pid < 0){
            $sql = "select {$fileds} from {$this->_table_menu} where isshow = 'Y' {$where} order by orderid desc, menuid asc";
        }else{
            $sql = "select {$fileds} from {$this->_table_menu} where isshow = 'Y' {$where} and parentid=:parentid order by orderid desc,menuid asc";
            $flag = true;
        }

        $this->_db->prepare($sql);
        if($type){
            $this->_db->bindValue(':type', $type, PDO::PARAM_INT);
        }
        if($isshow){
            $this->_db->bindValue(':isshow', $isshow, PDO::PARAM_STR);
        }

        if($flag){
            $this->_db->bindValue(":parentid", $parentid, PDO::PARAM_INT);
        }

        if($this->_db->execute()){
            $data = $this->_db->fetchAll(PDO::FETCH_ASSOC);
            if(! empty($data)){
                return $data;
            }
        }

        return '';
    }

    public function getMenuById($mid){
        if(! AppCommon::intParse($mid, $menuid) || $menuid < 0){
            return null;
        }

        $this->_db->prepare("select menuid,type, name,action_key,action,parentid,isshow from {$this->_table_menu} where menuid=:menuid limit 0,1 ");
        $this->_db->bindValue(":menuid", $menuid, PDO::PARAM_INT);
        if($this->_db->execute()){
            $data = $this->_db->fetchAll(PDO::FETCH_ASSOC);

            if(! empty($data)){
                return $data[0];
            }
        }

        return null;
    }

    public function addMenu($type, $name, $action_key, $url, $parentid, $isshow){
        $sql = "insert into {$this->_table_menu} (`name`,`type`,`action_key`,`action`,`parentid`,`isshow`) values(:name,:type,:action_key,:action,:parentid,:isshow)";
        $this->_db->prepare($sql);
        $this->_db->bindValue(":name", $name, PDO::PARAM_STR);
        $this->_db->bindValue(":type", $type, PDO::PARAM_INT);
        $this->_db->bindValue(':action_key', $action_key, PDO::PARAM_STR);
        $this->_db->bindValue(":action", $url, PDO::PARAM_STR);
        $this->_db->bindValue(":parentid", $parentid, PDO::PARAM_INT);
        $this->_db->bindValue(":isshow", $isshow, PDO::PARAM_STR);

        if($this->_db->execute()){
            return $this->_db->rowCount();
        }

        return false;
    }

    public function editMenu($mid, $type, $name, $action_key, $url, $pid, $isshow){
        if(! AppCommon::intParse($mid, $menuid) || $menuid <= 0 || ! AppCommon::intParse($pid, $parentid) || $parentid < 0){
            return null;
        }

        if(empty($name) || empty($isshow) || empty($type)){
            return null;
        }

        $sql = "update {$this->_table_menu} set `type`=:type, `name`=:name, `action_key`=:action_key,`action`=:action,`parentid`=:parentid,`isshow`=:isshow where menuid=:menuid ";
        $this->_db->prepare($sql);
        $this->_db->bindValue(":menuid", $menuid, PDO::PARAM_INT);
        $this->_db->bindValue(":type", $type, PDO::PARAM_INT);
        $this->_db->bindValue(":name", $name, PDO::PARAM_STR);
        $this->_db->bindValue(":action_key", $action_key, PDO::PARAM_STR);
        $this->_db->bindValue(":action", $url, PDO::PARAM_STR);
        $this->_db->bindValue(":parentid", $parentid, PDO::PARAM_INT);
        $this->_db->bindValue(":isshow", $isshow, PDO::PARAM_STR);

        if($this->_db->execute()){
            return $this->_db->rowCount();
        }

        return false;
    }

    /**
     * 根据key查询对应动作信息
     *
     * @param string $action_key
     * @return void mixed NULL
     */
    public function getMenuByKey($action_key){
        if(empty($action_key)){
            return;
        }

        $this->_db->prepare("select menuid,type, name,action_key,action,parentid,isshow from {$this->_table_menu} where action_key=:action_key limit 0,1 ");
        $this->_db->bindValue(":action_key", $action_key, PDO::PARAM_STR);
        if($this->_db->execute()){
            return $this->_db->fetch(PDO::FETCH_ASSOC);
        }

        return null;
    }

    public function getMenuByKeyList($action_key){
        if(empty($action_key)){
            return;
        }

        $this->_db->prepare("select menuid,type, name,action_key,action,parentid,isshow from {$this->_table_menu} where action_key=:action_key limit 0,100 ");
        $this->_db->bindValue(":action_key", $action_key, PDO::PARAM_STR);
        if($this->_db->execute()){
            return $this->_db->fetchAll(PDO::FETCH_ASSOC);
        }

        return null;
    }

    public function getUserRole($adminid){
        if(empty($adminid)){
            return false;
        }
        $data = array();
        $sql = 'select priv from ' . $this->_table_admin_role . ' where id=:adminid';

        $this->_db->prepare($sql);
        $this->_db->bindValue(':adminid', $adminid, PDO::PARAM_INT);
        if($this->_db->execute()){
            $row = $this->_db->fetch(PDO::FETCH_ASSOC);
            $data = unserialize($row['priv']);
        }
        return $data;
    }

    public function getRoleByName($roleName){
        if(empty($roleName)){
            return false;
        }
        $data = array();
        $sql = 'select role_id from ' . $this->_table_admin_role . ' where role_name=:role_name';

        $this->_db->prepare($sql);
        $this->_db->bindValue(':role_name', $roleName, PDO::PARAM_INT);
        if($this->_db->execute()){
            $row = $this->_db->fetch(PDO::FETCH_ASSOC);
            $data = unserialize($row['priv']);
        }
        return $data;
    }
}
